﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;


namespace MergeExcel.Common {

    public static class Extends {

        #region 字符串

        public static bool Empty(this string s)
        {
            if (s is null)
                return true;
            if (string.IsNullOrEmpty(s))
                return true;
            if (string.IsNullOrWhiteSpace(s))
                return true;
            return false;
        }

        public static int ToInt32(this string s)
        {
            if (s.Empty())
                return 0;
            return Convert.ToInt32(s);
        }

        public static decimal ToDecimal(this string s)
        {
            if (s.Empty())
                return 0;
            return Convert.ToDecimal(s);
        }

        public static DateTime ToDateTime(this string s)
        {
            return Convert.ToDateTime(s);
        }

        #endregion 字符串

        #region 日期

        public static DateTime ToSDate(this DateTime s)
        {
            return Convert.ToDateTime(s.ToString("yyyy-MM-dd 00:00:00"));
        }

        public static DateTime ToEDate(this DateTime s)
        {
            return Convert.ToDateTime(s.ToString("yyyy-MM-dd 23:59:59"));
        }

        public static DateTime Format(this DateTime d)
        {
            DateTimeFormatInfo format = new DateTimeFormatInfo
            {
                LongDatePattern = "yyyy-MM-dd",
                LongTimePattern = "HH:mm:ss"
            };
            return Convert.ToDateTime(d.ToString(), format);
        }

        public static DateTime? Format(this DateTime? d)
        {
            if (d is null)
                return d;
            DateTimeFormatInfo format = new DateTimeFormatInfo
            {
                LongDatePattern = "yyyy-MM-dd",
                LongTimePattern = "HH:mm:ss"
            };
            return Convert.ToDateTime(d?.ToString(), format);
        }

        public static TimeSpan DateDiff(this DateTime d, DateTime d1)
        {
            TimeSpan ts = new TimeSpan(d.Ticks);
            var ts1 = ts.Subtract(new TimeSpan(d1.Ticks));
            return ts1;
        }

        public static int LastDay(this DateTime d)
        {
            return DateTime.DaysInMonth(d.Year, d.Month);
        }

        public static DateTime ToMonthStart(this DateTime d)
        {
            DateTime d1 = new DateTime(d.Year, d.Month, 1, 0, 0, 0);
            return d1;
        }

        public static DateTime ToMonthEnd(this DateTime d)
        {
            DateTime d1 = new DateTime(d.Year, d.Month, 1, 0, 0, 0);
            return new DateTime(d1.Year, d1.Month, d.LastDay(), 23, 59, 59);
        }

        #endregion 日期

        #region Object

        public static bool ToInt32(this object obj, out int val)
        {
            val = int.MinValue;
            try
            {
                if (obj is null)
                    return false;
                val = Convert.ToInt32(obj);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static int ToInt32(this object obj)
        {
            try
            {
                if (obj is null || obj.ToString().Empty())
                    throw new Exception("无法解析");
                int val = Convert.ToInt32(obj);
                return val;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static uint ToUInt32(this object obj)
        {
            try
            {
                if (obj is null)
                    throw new Exception("无法解析");
                uint val = Convert.ToUInt32(obj);
                return val;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static double ToDouble(this object obj)
        {
            try
            {
                if (obj is null)
                    throw new Exception("无法解析");
                return Convert.ToDouble(obj);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static decimal ToDecimal(this object obj)
        {
            if (obj is null)
                throw new Exception("无法解析");
            return Convert.ToDecimal(obj);
        }

        public static DateTime ToDateTime(this object obj, string format = "")
        {
            if (obj is null || obj.ToString().Empty())
                throw new Exception("无法解析");
            if (format.Empty())
                return Convert.ToDateTime(obj);
            else
                return DateTime.ParseExact(obj.ToString(), format, CultureInfo.CurrentCulture);
        }

        public static DateTime? ToDateTimeNull(this object obj, string format = "")
        {
            if (obj is null)
                return null;
            if (obj.ToString().Empty())
                return null;
            if (format.Empty())
                return Convert.ToDateTime(obj);
            else
                return DateTime.ParseExact(obj.ToString(), format, CultureInfo.CurrentCulture);
        }

        #endregion Object

        #region LIST

        public static bool Empty<T>(this List<T> lst)
        {
            if (lst is null)
                return true;
            if (lst.Count < 1)
                return true;
            return false;
        }

        #endregion LIST
    }
}